package com.chunxiadong.controller.admin;

import com.chunxiadong.entity.Blog;
import com.chunxiadong.entity.FriendLink;
import com.chunxiadong.entity.Picture;
import com.chunxiadong.entity.Type;
import com.chunxiadong.service.BlogService;
import com.chunxiadong.service.FriendLinkService;
import com.chunxiadong.service.PictureService;
import com.chunxiadong.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/6
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    TypeService typeService;

    @Autowired
    BlogService blogService;

    @Autowired
    FriendLinkService friendLinkService;

    @Autowired
    PictureService pictureService;

    /**
     * admin_logout
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "admin/login";
    }


    /**
     * toBlog
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("/blog")
    public String toBlog(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        model.addAttribute("types", typeService.getAllType());
        PageInfo<Blog> pageInfo = blogService.getAllBlogToAdminByPage(pageNum, 5);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blog";
    }

    /**
     * toType
     * @param pageNum
     * @param attributes
     * @return
     */
    @RequestMapping("/type")
    public String toType(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model attributes) {
        PageInfo<Type> pageInfo = typeService.getAllTypeByPage(pageNum, 5);
        attributes.addAttribute("pageInfo", pageInfo);
        return "admin/type";
    }

    /**
     * toFriendLink
     * @return
     */
    @RequestMapping("/friendLink")
    public String toFriendLink(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model) {
        PageInfo<FriendLink> pageInfo = friendLinkService.getAllFriendLinkPageInfo(pageNum, 5);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/friendLink";
    }

    /**
     * toPicture
     * @return
     */
    @RequestMapping("/picture")
    public String toPicture(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model) {
        PageInfo<Picture> pageInfo = pictureService.getAllPicturePageInfo(pageNum, 5);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/picture";
    }

}
